﻿using System.Windows;
using AttachedCommands.ACB;
using Microsoft.Win32;
using Snippet2Vsi.Classes;
using SnippetEditor;

namespace Snippet2Vsi.ViewModels
{
    public class VsContentCreatorViewModel:ViewModelBase
    {
        #region Members

        private RelayCommand _browseVsCommand;
        private RelayCommand _browseSnippetCommand;
        private RelayCommand _convertCommand;
        private string _vsPath;

        #endregion

        #region Commands

        public RelayCommand BrowseVsCommand
        {
            get { return _browseVsCommand ?? (_browseVsCommand = new RelayCommand(BrowseVsCommandHandler)); }
            set { _browseVsCommand = value; }
        }

       

        public RelayCommand BrowseSnippetCommand
        {
            get { return _browseSnippetCommand ?? (_browseSnippetCommand = new RelayCommand(BrowseSnippetCommandHandler)); }
            set { _browseSnippetCommand = value; }
        }



        public RelayCommand ConvertCommand
        {
            get { return _convertCommand ?? (_convertCommand = new RelayCommand(ConvertCommandHandler, CanExecuteConvert)); }
            set { _convertCommand = value; }
        }

        private bool CanExecuteConvert(object obj)
        {
            return !string.IsNullOrEmpty(VsPath) && !string.IsNullOrEmpty(SnippetLocation);
        }

        #endregion

        #region Handlers

        private void ConvertCommandHandler(object obj)
        {
            TryCatch.BeginTryCatch(() => {
                if(CanExecuteConvert(null))
                {
                    VsiConverter.WriteVsContentFile(VsPath, new[] { SnippetLocation });
                    MessageBox.Show("Snippet converted to vscontent file successfully", "Success", MessageBoxButton.OK,
                                           MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("Please enter snippet and vscontent file location", "Error", MessageBoxButton.OK,
                                    MessageBoxImage.Error);
                }
              
            });

        }


        private void BrowseVsCommandHandler(object obj)
        {
            TryCatch.BeginTryCatch(BrowseVs);
        }

        private void BrowseSnippetCommandHandler(object obj)
        {
            TryCatch.BeginTryCatch(BrowseSnippet);
        }

       

        #endregion

        #region Methods

        private void BrowseSnippet()
        {
            var openFileDailog = new OpenFileDialog
            {
                CheckFileExists = true,
                Multiselect = false,
                Filter = "Code Snippets (.snippet)|*.snippet"
            };
            openFileDailog.ShowDialog();
            if (!string.IsNullOrEmpty(openFileDailog.FileName))
            {
                SnippetLocation = openFileDailog.FileName;
            }
        }

        private void BrowseVs()
        {
            var saveFileDialog = new SaveFileDialog
            {
                Filter = "VsContent (.vscontent)|*.vscontent"
            };
            saveFileDialog.ShowDialog();
            if (!string.IsNullOrEmpty(saveFileDialog.FileName))
            {
               VsPath = saveFileDialog.FileName;
            }
        }

        #endregion

        #region Properties

        
        private string _snippetLocation;

        public string SnippetLocation
        {
            get { return _snippetLocation; }
            set
            {
                _snippetLocation = value;
                NotifyPropertyChanged(this, o => o.SnippetLocation);
            }
        }
       

        public string VsPath
        {
            get { return _vsPath; }
            set
            {
                _vsPath = value;
                NotifyPropertyChanged(this, o => o.VsPath);
            }
        }

        #endregion
    }
}
